package com.angel.application
{
	import com.angel.syncronization.TransactionalBlock;
	import com.angel.utils.RemoteServicesNames;
	
	import mx.collections.IList;
	import mx.core.UIComponent;
	
	public class ApplicationLocator
	{
		private static const _instance:ApplicationLocator = new ApplicationLocator(SingletonLock);
		
 		public function ApplicationLocator(lock:Class) {
 			// verify that the lock is the correct class reference.
   			if ( lock != SingletonLock ) {
    			throw new Error( "Invalid Singleton access. Use ApplicationLocator.instance." );
    		}
 		}
 		
 		public static function get instance():ApplicationLocator {
	   		return _instance;
	   	}

		//TODO Agrega el user.

		[Bindable]
		public var languages:IList;
		
		[Bindable]
		private var _neighborhood:IList;
		
		[Bindable]
		private var _countries:IList;
		
		[Bindable]
		private var _hotels:IList;
		
		[Bindable]
		private var _configurationParameters:IList;
		
		/*
		public function get languages():IList {
			return _languages;
	   	}
	   	public function set languages(data:IList):void{
			_languages = data;
	   	}*/
	   	
	   	public function get countries():IList {
			return _countries;
	   	}
	   	public function set countries(data:IList):void{
			_countries = data;
	   	}
	   	
	   	public function get hotels():IList {
			return _hotels;
	   	}
	   	public function set hotels(data:IList):void{
			_hotels = data;
	   	}
	   	
	   	public function get neighborhood():IList {
			return _neighborhood;
	   	}
	   	public function set neighborhood(data:IList):void{
			_neighborhood = data;
	   	}
	   	
	   	public function get configurationParameters():IList {
			return _configurationParameters;
	   	}
	   	public function set configurationParameters(data:IList):void{
			_configurationParameters = data;
	   	}

		/**
		 * Inicializa las entidades basicas de la aplicacion para poder accederlas 
		 * desde cualquier lugar de la misma.
		 * 
		 */
		public function inicializar(uiComponent:UIComponent = null):void {
			if(!this.estaInicializado()){
				var transactionalBlock:TransactionalBlock = new TransactionalBlock();
				transactionalBlock.register(RemoteServicesNames.LANGUAGE_SERVICE_NAME, "findAllActives").callbackFunction(inicializarLanguagesCallbackFunction);
				transactionalBlock.register(RemoteServicesNames.CONFIGURATION_PARAMETER_SERVICE_NAME, "findAll").callbackFunction(inicializarParametrosConfiguracioCallbackFunction);
				transactionalBlock.execute(uiComponent);
			}
	   	}
	   	
	   	public function estaInicializado():Boolean {
			return _configurationParameters != null || languages != null || _neighborhood != null || _countries != null || _hotels != null;
	   	}

		private function inicializarHotelsCallbackFunction(data:IList):void{
			_hotels = data;				
		}

		private function inicializarCountriesCallbackFunction(data:IList):void{
			_countries = data;				
		}

		private function inicializarNeighborhoodsCallbackFunction(data:IList):void{
			_neighborhood = data;				
		}

		private  function inicializarLanguagesCallbackFunction(data:IList):void{
			languages = data;				
		}

		private  function inicializarParametrosConfiguracioCallbackFunction(data:IList):void{
			_configurationParameters = data;
		}

	 	public function clear():void{
	 		ApplicationLocator.instance.languages = null;
	 		ApplicationLocator.instance.neighborhood = null;
	 		ApplicationLocator.instance.configurationParameters = null;
	 		ApplicationLocator.instance.configurationParameters = null;
	 		ApplicationLocator.instance.countries = null;
	 		ApplicationLocator.instance.hotels = null;
	 	}
	 	
	 	
	 	
	}
}

class SingletonLock {
}
